Mongodb - J’suis pas venu là pour souffrir ok

Colin Fay | ThinkR
Rencontre R 2023

In a perfect world

But in an alternate universe

Mongodb?

In the beginning, there was a project

 

 

 

=>

 

 

 

=>

And then, another project

 

 

 

=>

 

 

 

=>

And then, another project

 

 

 

<=>

 

 

 

<=>

What’s wrong with mongodb & R?

 

 

 

=>

 

 

 

=>

 

fridge <- mongolite::mongo()
monday_meal <- list(
  day = Sys.Date() + 1,
  ingredient = c("tofu", "brocoli"),
  to_reheat = TRUE,
  box = "blue"
)

fridge$insert(monday_meal)
List of 6
 $ nInserted  : int 1
 $ nMatched   : int 0
 $ nModified  : int 0
 $ nRemoved   : int 0
 $ nUpserted  : int 0
 $ writeErrors: list()

 

class(monday_meal)
[1] "list"

 

fridge_find <- fridge$find()

class(fridge_find)
[1] "data.frame"

 

fridge_iterate <- fridge$iterate()$one()

class(fridge_iterate)
[1] "list"

dplyr::glimpse(monday_meal)
List of 4
 $ day       : Date[1:1], format: "2023-06-22"
 $ ingredient: chr [1:2] "tofu" "brocoli"
 $ to_reheat : logi TRUE
 $ box       : chr "blue"
dplyr::glimpse(fridge_find)
Rows: 1
Columns: 4
$ day        <list> "2023-06-22"
$ ingredient <list> <"tofu", "brocoli">
$ to_reheat  <list> TRUE
$ box        <list> "blue"
dplyr::glimpse(fridge_iterate)
List of 4
 $ day       :List of 1
  ..$ : chr "2023-06-22"
 $ ingredient:List of 2
  ..$ : chr "tofu"
  ..$ : chr "brocoli"
 $ to_reheat :List of 1
  ..$ : logi TRUE
 $ box       :List of 1
  ..$ : chr "blue"

 

tuesday_meal <- list(
  day = Sys.Date() + 2,
  ingredient = "pasta",
  box = "red"
)

fridge$insert(tuesday_meal)
List of 6
 $ nInserted  : int 1
 $ nMatched   : int 0
 $ nModified  : int 0
 $ nRemoved   : int 0
 $ nUpserted  : int 0
 $ writeErrors: list()

dplyr::glimpse(
  fridge$iterate('{"box": "blue"}')$one()
)
List of 4
 $ day       :List of 1
  ..$ : chr "2023-06-22"
 $ ingredient:List of 2
  ..$ : chr "tofu"
  ..$ : chr "brocoli"
 $ to_reheat :List of 1
  ..$ : logi TRUE
 $ box       :List of 1
  ..$ : chr "blue"
dplyr::glimpse(
  fridge$iterate('{"box": "red"}')$one()
)
List of 3
 $ day       :List of 1
  ..$ : chr "2023-06-23"
 $ ingredient:List of 1
  ..$ : chr "pasta"
 $ box       :List of 1
  ..$ : chr "red"

{mongooser}

Food <- mongooser::model(
  "Food",
  properties = list(
    day = \(x) structure(NA_real_, class = "Date"),
    ingredient = character,
    box = character,
    to_reheat = logical
  ),
  validator = list(
    day = lubridate::ymd,
    ingredient = as.character,
    box = as.character,
    to_reheat = as.logical
  )
)
monday <- Food$new(
  monday_meal
)
monday$save()
List of 6
 $ nInserted  : int 1
 $ nMatched   : int 0
 $ nModified  : int 0
 $ nRemoved   : int 0
 $ nUpserted  : int 0
 $ writeErrors: list()
tuesday <- Food$new(
  tuesday_meal
)
tuesday$save()
List of 6
 $ nInserted  : int 1
 $ nMatched   : int 0
 $ nModified  : int 0
 $ nRemoved   : int 0
 $ nUpserted  : int 0
 $ writeErrors: list()

{mongooser}

dplyr::glimpse(
  Food$find()
)
List of 2
 $ :List of 4
  ..$ day       : Date[1:1], format: "2023-06-22"
  ..$ ingredient: chr [1:2] "tofu" "brocoli"
  ..$ box       : chr "blue"
  ..$ to_reheat : logi TRUE
 $ :List of 4
  ..$ day       : Date[1:1], format: "2023-06-23"
  ..$ ingredient: chr "pasta"
  ..$ box       : chr "red"
  ..$ to_reheat : logi FALSE

pak::pak(“thinkr-open/mongooser”)

What I learned

1. Never take shortcuts

2. Flexibility doesn’t work in production

Thanks. Question?